package com.vp.org.dao.impl;

import com.vp.org.constant.Sql;
import com.vp.org.dao.RecordDao;
import com.vp.org.pojo.Record;
import com.vp.org.util.JdbcUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;

public class RecordDaoImpl extends JdbcUtil implements Sql, RecordDao {

    // 删除兑换记录
    // delete from record where id = ?;

    @Override
    public int deleteRecord(Record record) {
        return template.update(RECORD_DELETERECORD,record.getId());
    }

    // 记录总数
    // select count(id) count from record;
    @Override
    public long count() {
        return (long)template.queryForMap(RECORD_COUNT).get("count");
    }

    // 记录总页数
    @Override
    public int recordPage() {
        int count = (int) count();
        return count % 4 == 0 ? count / 4 : count / 4 + 1;
    }

    // 分页查询兑换记录
    // select * from record limit 0,4;
    @Override
    public List<Record> queryPage(int page) {
        return template.query(RECORD_QUERYPAGE,new BeanPropertyRowMapper<>(Record.class),(page - 1) * 4);
    }
}
